<?php
/**
 * tpAdmin [a web admin based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpadmin.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 配置
//-------------------------

namespace app\admin\controller;

use app\admin\Controller;
use think\Db;


class TaskRelease extends Controller
{
    //行业列表
    public function index()
    {
        if(input('status')){
            $where['a.status'] = input('status');
        }else{
            $where['a.status'] = ['in',[1,2,3,4,5,6]];
        }
        $list = Db::name('task_release')->alias('a')
            ->join('tp_user b', 'a.uid = b.id', 'left')
            ->field('b.nickname,a.id,a.title,a.unit_price,a.num,a.uid,a.receive_num,a.status,a.add_time')
            ->order('a.id desc')
            ->where($where)
            ->paginate(10, false, ['query' => $this->request->get()]);
        return view('task_release/index', [
            'list' => $list,
            'page' => $list->render(),
            'count' => $list->total()
        ]);
    }

    //编辑
    public function edit()
    {
        if ($this->request->post()) {
            $data = input('post.');
            $info = [
                'status' => ($data['type'] == 1) ? 2 : 6,
                'reason' => ($data['type'] == 1) ? '' : $data['reason']
            ];
            if($data['type']== 2 && !$data['reason']){
                return ajax_return_error('请填写失败原因');
            }
            $infos = Db::name('task_release')->where(['id' => $data['id']])->find();
            $tims = strtotime($infos['end_time'])-time();
            if($tims < 60 * 60){
                return ajax_return_error('离任务结束时间不足一小时，无法发布');
            }
            if ($data['type'] == 2) {
                //调用扣除发布的金额的接口
                $rate = [1 => 0.8, 2 => 1, 3 => 1.2];
                $account = $infos['unit_price'] * $infos['num'] * $rate[$infos['type']] * 100;
                $res = auditTaskFail(config('urls') . '/account/addaccount', $infos['title'], $infos['uid'], $account, $infos['id']);
                if (!$res) {
                    return ajax_return_error('失败');
                }
            }
            $result = Db::name('task_release')->where(['id' => $data['id']])->update($info);
            if ($result === false) {
                return ajax_return_error('失败');
            }
            //发布一次悬赏任务
            $taskRelease = Db::name('task_release')->where(['uid' => $infos['uid'], 'status' => 2])->count();
            if ($taskRelease == 1) {
                //新手任务----发布一次悬赏任务
                $signin = dailyTasks(config('urls') . '/account/addaccount', '发布一次悬赏任务', $infos['uid'], 20);
                if (!$signin) {
                    return ajax_return_adv(1002);
                }
            }
            return ajax_return_adv(1000);
        } else {
            $info = Db::name('task_release')->alias('a')
                ->join('tp_user b', 'a.uid = b.id', 'left')
                ->field('b.nickname,a.*')
                ->where(['a.id' => input('id')])->find();
            $info['case_img'] = json_decode(json_decode($info['case_img']));
            $info['qrcode_img'] = json_decode(json_decode($info['qrcode_img']));

            return view('task_release/edit', ['info' => $info]);
        }
    }
    //详情
    public function detail()
    {
        $info = Db::name('task_release')->alias('a')
            ->join('tp_user b', 'a.uid = b.id', 'left')
            ->field('b.nickname,a.*')
            ->where(['a.id' => input('id')])->find();
        $info['case_img'] = json_decode(json_decode($info['case_img']));
        $info['qrcode_img'] = json_decode(json_decode($info['qrcode_img']));
        return view('task_release/detail', ['info' => $info]);
    }

    //任务结余
    public function taskjieyu(){
        $data = input('post.');
        $result = surplus(config('xsurls').'/job/taskjieyu',$data['uid'],$data['id']);
        if($result && $result['errcode'] == 0){
            $re = Db::name('task_release')->where(['id'=>$data['id']])->update(['status'=>3]);
            if ($re !== false) {
                return ajax_return_adv(1000);
            }
            return ajax_return_adv(1002,'','失败');
        }
        return ajax_return_adv(1002,'',$result && $result['errmsg']?$result['errmsg']:'失败' );
    }

}